package com.imooc.bean;

/**
 * @Date:2021/10/12/21:32
 * @Description:
 */
public class User implements Comparable {
    private String name;
    private int age;
    public User(){

    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //按照姓名从小到大排列
    @Override
    public int compareTo(Object o) {
        //1-判断类型
        if(o instanceof User){
            User user = (User)o;
            int i = -this.name.compareTo(user.name);//负号表示从大到小排列

            if(i != 0){
                return i;
            }else {
                return -Integer.compare(this.age, user.age);
            }


        } else{
            throw new RuntimeException("输入的类型不匹配");
        }


    }
}
